package com.cx.common.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cx.common.mybatisplus.entity.BaseEntity;
import com.cx.common.secuity.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.Instant;

/**
 * @author _
 * @date 2022/1/6
 */
@Slf4j
@Component
public class CreateUpdateTimeMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = SecurityUtils.getUserId();
        Integer sysTem = SecurityUtils.getSysTem();
        this.strictInsertFill(metaObject, BaseEntity.CREATE_TIME, Long.class, Instant.now().getEpochSecond());
        this.strictInsertFill(metaObject, BaseEntity.UPDATE_TIME, Long.class, Instant.now().getEpochSecond());
        this.strictInsertFill(metaObject, BaseEntity.CREATE_BY, Long.class, userId);
        this.strictInsertFill(metaObject, BaseEntity.UPDATE_BY, Long.class, userId);
        this.strictInsertFill(metaObject, BaseEntity.SYSTEM, Integer.class, sysTem);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, BaseEntity.UPDATE_TIME, Long.class, Instant.now().getEpochSecond());
        this.strictInsertFill(metaObject, BaseEntity.UPDATE_BY, Long.class, SecurityUtils.getUserId());
        this.strictInsertFill(metaObject, BaseEntity.SYSTEM, Integer.class, SecurityUtils.getSysTem());
    }
}
